using CNRegex.Entity;
using NHibernate.Linq;
using System.Linq;
using CNRegex.Entity.EntityEx;
using Project.Common;
using System;
using System.Collections.Generic;
using NHibernate;
namespace CNRegex.Dal.DataAccess {
	public partial class DalRegexFavourite 
	{
		private void Initialize()
		{

		}
		public IList<RegexExpression> PaginateFavoriteExpression(int? pageSize, int? pageIndex, Guid? regexCategoryID, Guid? userID, ref int count)
		{
			pageSize = pageSize ?? ProjectGlobal.PAGE_SIZE;
			var query = (from t1 in Session.Query<RegexExpression>()
						 join t2 in Session.Query<RegexFavourite>()
						 on t1.ID equals t2.FavoriteID
						 orderby t2.FavouriteDate descending
						 where (regexCategoryID == null || t1.CategoryID == regexCategoryID) && (userID == null || userID == t1.UserID)
						 select new RegexExpression()
						 {
							 CategoryID = t1.CategoryID,
							 ClickCount = t1.ClickCount,
							 CreateDate = t1.CreateDate,
							 Description = t1.Description,
							 Expression = t1.Expression,
							 ID = t1.ID,
							 Matches = t1.Matches,
							 NonMatches = t1.NonMatches,
							 Rating = t1.Rating,
							 Title = t1.Title,
							 UserID = t1.UserID
						 }).Skip((pageIndex ?? 0) * pageSize.Value).Take(pageSize.Value);
			var queryCount = (from t1 in Session.Query<RegexExpression>()
							  join t2 in Session.Query<RegexFavourite>()
							  on t1.ID equals t2.FavoriteID
							  orderby t2.FavouriteDate descending
							  where (regexCategoryID == null || t1.CategoryID == regexCategoryID) && (userID == null || userID == t1.UserID)
							  select t1.ID);
			count = queryCount.Count();

			return query.ToList();
		}
	}
}
		